<?php
class LeksController extends Controller {

	public function indexAction() {
        $this->checkLoginRedirect();

		$user = $this->_session->getCurrent();
		$userSettings = new UserSettings($user->id);
		if($userSettings->getPreferDict() != 0) {
			$dictionary = new Dictionary($userSettings->getPreferDict());
		}

		$filterLang = $this->_request->getString('filterLang');
		
        if(!$filterLang) {
            $filterLang = $this->_session->getString('filterLang');
			if(!$filterLang) {
				$filterLang = ($userSettings->getPreferDict() == 0) ? 'all' : $dictionary->name;
				$this->_session->set('filterLang', $filterLang);
			}
            //$this->_session->delete('filterLang');
        }

        $preparedLeks = array();
		$leks = $this->_getAllLeks();
        if($filterLang && $filterLang != 'all') {
            $this->_session->set('filterLang', $filterLang);
            $dict = new Dictionary();
            $dict->loadByName($filterLang);
            foreach ($leks as $leksem) {
                $leksem->translated = $leksem->isTranslatedInto($dict->id);
                $preparedLeks[] = $leksem;
            }
        }
        else {
            $this->_session->delete('filterLang');
			$filterLang = '';
            $preparedLeks = $leks;
        }
        
        $dicts = $this->_getAllDict();
        $currPage = $this->_request->getInt('page');
        if(empty($currPage)) {
            $currPage = 1;
        }
        $pager = new Pager();

		$pager->setOptions(array(
			'perPage' => $userSettings->getLeksPerPage(),
            'data' => $preparedLeks
        ));
        $leksems = $pager->getItems($currPage);
        $this->_view->assignArray(array(
			'pageTitle' => 'Localization system :: Leksem List',
			'pageId' => 'lekslist',
			'nextPage' => $pager->getNextPage($currPage),
			'prevPage' => $pager->getPrevPage($currPage),
			'totalPages' => $pager->getTotal(),
			'currPage' => $currPage,
            'left' => $pager->getNearest($currPage, 'left'),
            'right' => $pager->getNearest($currPage, 'right'),
			'lastPage' => $pager->getLastPage(),
			'firstPage' => $pager->getFirstPage(),
            'dicts' => $dicts,
			'leks' => $leksems,
			'filterLang' => $filterLang
		));
		$this->_view->render('leks/index');
	}
	
	public function addleksAction() {
        $this->checkLoginRedirect();
        $this->setErrors();
		$this->_view->assignArray(array(
			'pageTitle' => 'Localization system :: Add leksem',
            'pageId' => 'leksadd'
		));
		$this->_view->render('leks/add');
	}
	
	public function addAction() {
        $this->checkLoginRedirect();
		$leksName = $this->_request->getString('leksname');
        $leksContext = $this->_request->getString('lekscontext');
		$leksBaseVal = $this->_request->getString('leksbase');
		if($this->_model->isLeksNameExists($leksName)) {
			$this->addError('Leksem with same name are already exists');
		}
		if(empty($leksName)) {
            $this->addError('Leksem name can not be empty');
        }
        if(empty($leksBaseVal)) {
            $this->addError('Leksem value can not be empty');
        }
		if(!$this->isErrors()) {
			$leksem = new Leksem();
			$leksem->name = $leksName;
            $leksem->context = $leksContext;
			$leksem->userId = $this->_currentUser->id;
			$leksem->value = $leksBaseVal;
			$leksem->websiteId = $this->_session->getInt('websiteId');
			$leksem->save();
			$this->delegateToAction('addmore');
		}
        else {
            $this->delegateToAction('addleks');
        }
	}
	
	public function addmoreAction() {
        $this->checkLoginRedirect();
		$this->_view->assignArray(array(
			'pageTitle' => 'Localization system :: Add more?',
            'pageId' => 'moreleksadd'
		));
		$this->_view->render('leks/addmore');
	}

	private function _getAllLeks() {
		$wsId = $this->_session->getInt('websiteId');
        if(empty($wsId)) {
            $this->redirect('website');
        }
		return $this->_model->getAllLeks($wsId);			
	}
	
	private function _getAllDict() {
		return $this->_model->getAllDict();			
	}
	
	public function translateAction() {
        $this->checkLoginRedirect();
        $this->setErrors();
		$leksemId = $this->_request->getInt('id');
        if(empty($leksemId)) {
            $leksemId = $this->_request->getInt('leksId');
        }
		$leksem = new Leksem($leksemId);
		$dicts = $this->_getAllDict();
		$this->_view->assignArray(array(
			'dicts' => $dicts,
			'leksem' => $leksem,
			'pageTitle' => 'Localization system :: Translate leksem',
            'pageId' => 'translate'
		));
		$this->_view->render('leks/translate');
	}
	
	public function dotranslateAction() {
        $this->checkLoginRedirect();
		$leksId = $this->_request->getInt('leksId');
		$dictId = $this->_request->getInt('dict');
        if($dictId == 0) {
            $this->addError('Please, choose your language.');
        }
		$value = $this->_request->getString('text');
        if(empty($value)) {
            $this->addError('Please put your translation.');
        }
        if(!$this->getErrors()) {
			$this->_session->set('leksId', $leksId);
            $translator = new Translator($dictId, $leksId);
			$translationExists = $translator->getTranslation();
			if(!empty($translationExists)) {
				$translator->updatetranslation($value);
			}
			else {
				if ($translator->saveTranslation($value)) {
					$leks = new Leksem($leksId);
					$leks->markTranslated();
				}
			}
			$this->redirect('leks');
        }
        else {
            $this->delegateToAction('translate');
        }
	}

	/*public function translatemoreAction() {
		$this->checkLoginRedirect();
		$leksId = $this->_session->getInt('leksId');
		$this->_session->delete('leksId');
		$this->_view->assignArray(array(
			'pageTitle' => 'Localization system :: Translate more?',
			'leksId' => $leksId,
            'pageId' => 'morelekstrans'
		));
		$this->_view->render('leks/translatemore');
	}*/

	public function viewAction() {
        $this->checkLoginRedirect();
		$leksemId = $this->_request->getInt('id');
		$leksem = new Leksem($leksemId);
		$dicts = $this->_getAllDict();
		$this->_view->assignArray(array(
			'dicts' => $dicts,
			'leksem' => $leksem,
			'pageTitle' => 'Localization system :: View translation',
            'pageId' => 'viewtranslation'
		));
		$this->_view->render('leks/view');
	}
	
	public function deleteAction() {
        $this->checkLoginRedirect();
		$leksemId = $this->_request->getInt('id');
		$leksem = new Leksem($leksemId);
		$leksem->delete();
		$this->redirect('leks');
	}
	
	public function editAction() {
        $this->checkLoginRedirect();
		$leksemId = $this->_request->getInt('id');
		$leksem = new Leksem($leksemId);
		$this->_view->assignArray(array(
			'leks' => $leksem,
			'pageTitle' => 'Localization system :: Edit leksem',
            'pageId' => 'leksedit'
		));
		$this->_view->render('leks/edit');
	}
	
	public function runeditAction() {
        $this->checkLoginRedirect();
		$leksemId = $this->_request->getInt('lId');
		$leksem = new Leksem($leksemId);
		$newVal = $this->_request->getString('leksbase');
        if(empty($newVal)) {
           $this->addError('Leksem value can not be empty');
        }
		$newName = $this->_request->getString('leksname');
        $newContext = $this->_request->getString('lekscontext');
        if(empty($newName)) {
           $this->addError('Leksem name can not be empty');
        }
        if(!$this->isErrors()) {
			$this->_session->set('leksId', $leksem->id);
            $leksem->name = $newName;
            $leksem->context = $newContext;
            $leksem->value = $newVal;
            $leksem->save();
			$this->delegateToAction('editmore');
        }
        else {
            $this->delegateToAction('edit');
        }
    }

	public function editmoreAction() {
		$this->checkLoginRedirect();
		$leksId = $this->_session->getInt('leksId');
		$this->_session->delete('leksId');
		$this->_view->assignArray(array(
			'pageTitle' => 'Localization system :: Edit more?',
			'leksId' => $leksId,
            'pageId' => 'moreleksedit'
		));
		$this->_view->render('leks/editmore');
	}
}